package com.itydl.a04.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * @author ydl
 * @version 带视察效果的ListView
 */

public class ParallaxListView extends ListView {

    private static final String TAG = "ParallaxListView";
    private ImageView ParallaxHeaderImageView;//头部视视差效果的图片
    private int mOldHeight;//此时ImageView的高度
    private int mRealHeight;//图片的真正高度

    public ParallaxListView(Context context) {
        this(context,null);
    }

    public ParallaxListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setHeadImageView(ImageView ivHeadImageView) {
        this.ParallaxHeaderImageView = ivHeadImageView;
        mOldHeight = ParallaxHeaderImageView.getMeasuredHeight();
//        int height = ParallaxHeaderImageView.getHeight();
//        Log.e(TAG,mOldHeight+"----"+height);//两个值一样
        mRealHeight = ParallaxHeaderImageView.getDrawable().getIntrinsicHeight();
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        System.out.println("deltaY: "+ deltaY + " scrollY: " + scrollY
                + " scrollRangeY: " + scrollRangeY
                + " maxOverScrollY: " + maxOverScrollY
                + " isTouchEvent: " + isTouchEvent);
        // deltaY : 竖直方向的瞬时偏移量, 顶部下拉- , 底部上拉+, 速度越快绝对值越大.
        // scrollY : 竖直方向ListView滚动的距离, 顶部- , 底部+
        // scrollRangeY : 竖直方向滚动距离
        // maxOverScrollY : 最大的超出滚动范围
        // isTouchEvent : 是否是手指触摸到顶部/底部. true触摸, false惯性
        if(deltaY <0 && isTouchEvent){
            int newHeight = ParallaxHeaderImageView.getHeight() + Math.abs(deltaY)/2;//减少拖动的距离
            if(newHeight <= mRealHeight){
                //只有在滑动的距离小于图片的真正高度的时候才去重新设置，超过了图片的高度就不去设置了。
                setCurrentImageViewHeight(newHeight);
            }
        }
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    private void setCurrentImageViewHeight(int newHeight) {
        ParallaxHeaderImageView.getLayoutParams().height = newHeight;
        ParallaxHeaderImageView.requestLayout();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
                case MotionEvent.ACTION_UP:
                    int startHeight = ParallaxHeaderImageView.getHeight();
                    ValueAnimator animator = ValueAnimator.ofInt(startHeight, mOldHeight);
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            // 500 毫秒内多次调用
                            // 0.0 -> 1.0
                            System.out.println("animation: " + animation.getAnimatedFraction());

                            // 模拟出来的中间值
                            Object animatedValue = animation.getAnimatedValue();
                            System.out.println("animation: " + animatedValue);

                            // 让新的高度生效
                            setCurrentImageViewHeight((Integer)animatedValue);
                        }
                    });
                    animator.setInterpolator(new OvershootInterpolator(4));
                    animator.setDuration(500);
                    animator.start();
                    break;

                default:
                    break;
         }
        return super.onTouchEvent(ev);
    }
}
